import os
from datetime import datetime
from pptx import Presentation
# 修正导入路径：TextFrame实际位于pptx.text.text模块
from pptx.text.text import TextFrame
from pptx.util import Inches

def edit_ppt(input_path, target, text, mode):
    try:
        prs = Presentation(input_path)
        date = datetime.now().strftime("%m.%d")
        for slide in prs.slides:
            for shape in slide.shapes:
                if shape.has_text_frame:
                    text_frame: TextFrame = shape.text_frame
                    if target in text_frame.text:
                        if mode == 0:  # 班级＋时间（添加换行）
                            text_frame.text = text_frame.text.replace(target, f"{text}\n{date}")  # 关键修改：添加\n
                        if mode == 1:  # 仅班级
                            text_frame.text = text_frame.text.replace(target, text)
                        if mode == 2:  # 仅时间
                            text_frame.text = text_frame.text.replace(target, date)
        os.makedirs("running", exist_ok=True)
        output_file_name = "running.pptx"
        prs.save(os.path.join("running", output_file_name))
    except Exception as e:
        print(f"处理PPT时出错: {e}")
        raise
